package com.diqizu.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.diqizu.dao.DisPlayDaoImpl;
import com.diqizu.entity.DisPlay;
import com.diqizu.entity.ProType;
import com.diqizu.servlet.BaseServlet;
import com.diqizu.utli.Result;

/**
 * Servlet implementation class chanpinductServletadmin
 */
@WebServlet("/admin/ChanPinDuctServletadmin")
public class ChanPinDuctServletadmin extends BaseServlet {

	DisPlayDaoImpl dao = new DisPlayDaoImpl();

	public void getProList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.获取页面传来的参数
		String name = req.getParameter("name");
		String productTypeId = req.getParameter("productTypeId");
		String minPrice = req.getParameter("minPrice");
		String maxPrice = req.getParameter("maxPrice");
		String page = req.getParameter("page");
		String limit = req.getParameter("limit");

		// 2.调用dao层的方法获取数据，当前页
		ArrayList<DisPlay> list = dao.chaproduct(name, productTypeId, minPrice, maxPrice, page, limit);

		// 2.调用dao层的方法 获取 符合条件的总条数的数据
		ArrayList<DisPlay> list2 = dao.chaproduct(name, productTypeId, minPrice, maxPrice, null, null);
		int total = list2.size();

		// 将数据封装为layu需要格式
		String res = Result.toClient(0, "产品列表", list, total);

		// 3.将数据返回到页面
		PrintWriter out = resp.getWriter();
		out.write(res);
		out.flush();
		out.close();
	}

	public void insertproduct(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 获取数据
		String name = req.getParameter("name");
		String Price = req.getParameter("Price");
		String Brand = req.getParameter("Brand");
		String proUrl = req.getParameter("proUrl");
		String dingwei = req.getParameter("dingwei");
		String productTypeId = req.getParameter("productTypeId");
		String meterial = req.getParameter("meterial");
		String diZhi = req.getParameter("diZhi");
		String style = req.getParameter("style");
		String isShowHome = req.getParameter("isShowHome");
		String sPrice = req.getParameter("sPrice");

		DisPlay product = new DisPlay(0, name, style, "", Brand, Price, "", meterial, diZhi,
				Integer.valueOf(productTypeId), proUrl, isShowHome, "", sPrice);

		boolean res = dao.insertproduct(product);

		PrintWriter out = resp.getWriter();
		if (res) {
			out.write(Result.toClient(0, "成功"));
		} else {
			out.write(Result.toClient(1, "失败"));
		}
		out.flush();
		out.close();

	}

	public void updateproduct(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 获取数据
		String name = req.getParameter("name");
		String Price = req.getParameter("Price");
		String Brand = req.getParameter("Brand");
		String proUrl = req.getParameter("proUrl");
		String dingwei = req.getParameter("dingwei");
		String productTypeId = req.getParameter("productTypeId");
		String meterial = req.getParameter("meterial");
		String diZhi = req.getParameter("diZhi");
		String style = req.getParameter("style");
		String isShowHome = req.getParameter("isShowHome");
		String sPrice = req.getParameter("sPrice");
		String proid = req.getParameter("proid");
		int proid2 = Integer.valueOf(proid);
		DisPlay product = new DisPlay(proid2, name, style, "", Brand, Price, dingwei, meterial, diZhi,
				Integer.valueOf(productTypeId), proUrl, isShowHome, "", sPrice);
		boolean res = dao.updateproduct(product);
		PrintWriter out = resp.getWriter();
		if (res) {
			out.write(Result.toClient(0, "修改成功"));
		} else {
			out.write(Result.toClient(1, "修改失败"));
		}
		out.flush();
		out.close();
	}

	public void deleteproduct(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 获取数据

		String proid = req.getParameter("proid");
		int proid2 = Integer.valueOf(proid);

		boolean res = dao.deleteproduct(proid2);

		PrintWriter out = resp.getWriter();
		if (res) {
			out.write(Result.toClient(0, "修改成功"));
		} else {
			out.write(Result.toClient(1, "修改失败"));
		}
		out.flush();
		out.close();

	}

	public void getProTypeList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 2.调用dao层的方法获取数据，当前页
		ArrayList<ProType> list = dao.gettype();
		String res = Result.toClient(0, "类型列表", list);
		// 2.将数据返回到页面
		PrintWriter out = resp.getWriter();
		out.write(res);
		out.flush();
		out.close();

	}

}
